<?php

function usermanage_getxml(){
  $services = array(
      '1' => 'get_forgetpasswd_xml',
      '2' => 'get_changepasswd_xml',
  );
  $type= (isset($_REQUEST['type']))? $_REQUEST['type'] : '1';
  $type = (array_key_exists($type, $services)) ? $type : '1';
  $call_back = $services[$type];

  $arg = $_REQUEST;
  call_user_func_array($call_back, array('arg'=>$arg));

}

function get_forgetpasswd_xml($params){
  global $base_path;
  $txtUserID = htmlspecialchars($params['uid']);
  $txtUserID  = '<userId>'.$txtUserID.'</userId>';


  $xml_data = <<<EOD
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><HTTPRequestMessage><Header><MessageIdentification ID="123450fdgk0305kf39059tkg94jkg86ky" TimeStamp="2011-01-01 12:00:00" Expiry="mm" /><ClientIdentification ID="ClientID" UserID="userid" Password="pwd" authenticationToken="encryptedtoken" />
  <MessageProcessing dataEncryption="false" processortype="NotifyUpdatePasswordPassback" processedReceipt="true" returnErrorToSender="true" />
  <URLInfo httpServer="intranet.crownrelo.com" />
</Header>
<DataSet>
<DataObject name="UpdatePasswordData" versionNo="1.0">
    $txtUserID
  <clientURL>$base_path</clientURL>
  </DataObject>
</DataSet>
</HTTPRequestMessage>
EOD;


  $url = USERMANAGE_API . "/generatePassword";

  $method = 'post';
  $arg = array();
  $data = 'requestXML='.urlencode($xml_data);
  $myrs = mycurl($url,$method,$arg,$data,1,'',true);
  $xml = simplexml_load_string($myrs['body']);
  
  $xml_header = $myrs['header'];
  if(isset($xml_header['http_code']) && $xml_header['http_code']===200 ){
      $xml->statusCode = '200' ;
      $xml->statusMessage = '' ;
  }
  else{
    $xml->statusCode = '' ;
    $xml->statusMessage = t('Network Problem. Please try again later.') ;
  }

  $statusCode = $xml->statusCode ;
  $statusMessage = $xml->statusMessage ;
  

  echo '{"statusCode":"'.$statusCode.'","statusMessage":"'.$statusMessage.'"}';
  exit;

}




function get_changepasswd_xml($params){
  global $user,$base_path;

  $currentPassword = htmlspecialchars($params['curpasswd']);
  $newPassword = htmlspecialchars($params['newpasswd']);


  $xml_data2 = <<<EOD
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><HTTPRequestMessage><Header><MessageIdentification ID="123450fdgk0305kf39059tkg94jkg86ky" TimeStamp="2011-01-01 12:00:00" Expiry="mm" /><ClientIdentification ID="ClientID" UserID="userid" Password="pwd" authenticationToken="encryptedtoken" />
  <MessageProcessing dataEncryption="false" processortype="NotifyUpdatePasswordPassback" processedReceipt="true" returnErrorToSender="true" />
  <URLInfo httpServer="intranet.crownrelo.com" />
</Header>
<DataSet>
<DataObject name="UpdatePasswordData" versionNo="1.0">
    <newPassword>$newPassword</newPassword>
  <currentPassword>$currentPassword</currentPassword>
        <clientURL>$base_path</clientURL>
  </DataObject>
</DataSet>
</HTTPRequestMessage>
EOD;


  $url = USERMANAGE_API . "/updatePassword";

  $method = 'post';
  $arg = array();
  $data = 'requestXML='.urlencode($xml_data2);
  //error_log($xml_data2."\n",3,'c:/tmp/pswd_data.log');

  $addtional_header = sprintf('Authorization: Basic %s',base64_encode($user->name.':'.$currentPassword));
  //error_log($user->name.':'.$currentPassword."\n",3,'c:/tmp/pswd_header.log');
  
  $myrs = mycurl($url,$method,$arg,$data,1,$addtional_header,true);

  $xml = simplexml_load_string($myrs['body']);
  //error_log(var_export($xml,TRUE)."\n",3,'c:/tmp/pswd_rs.log');
  
  $xml_header = $myrs['header'];
  if( isset($xml_header['http_code']) && $xml_header['http_code']===401 ){
    $xml->statusCode = '' ;
    $xml->statusMessage = t('Current Password provided for @email is not correct.',array('@email'=>$user->name)) ;
    
  }else if(isset($xml_header['http_code']) && $xml_header['http_code']===200 ){
    if(!isset($xml->statusCode)){
      $xml->statusCode = '' ;
      $xml->statusMessage = t('Internal Server Error.') ;
    }else if($xml->statusCode == 802){
      $xml->statusCode = '802' ;
      $xml->statusMessage = t('Current Password provided for @email is not correct.',array('@email'=>$user->name)) ;
    }
  }
  else{
    $xml->statusCode = '' ;
    $xml->statusMessage = t('Network Problem. Please try again later.') ;
  }

 
  $statusCode = $xml->statusCode ;
  $statusMessage = $xml->statusMessage ;
  


  echo '{"statusCode":"'.$statusCode.'","statusMessage":"'.$statusMessage.'"}';
  exit;

}






